<%@page import="java.util.*" import="java.sql.ResultSet" import="java.sql.Statement" import="java.sql.Connection" import="connection.DBConnection" language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<%@page import="java.text.SimpleDateFormat" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Journal Voucher</title>
<script language="javascript" type="text/javascript">

function checkDate()
{
	var c1;
	
  	var datevalid=/^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
  		
        c1= document.f14.rdate.value;

        if(c1.length==0)
        {
        	alert("Enter Date");
            return false;
      	 }
        if(!c1.match(datevalid))
        {
            alert("Please Enter Date in DD/MM/YYYY Format");
            document.f14.rdate.value="";
            document.f14.rdate.focus();
            
	         return false;
    	 }
        else
        {
        	dateval=c1.match(datevalid);
        }
        
        var dtYear = dateval[1];        
        dtMonth = dateval[3];
        dtDay=  dateval[5];

     if (dtMonth < 1 || dtMonth > 12) 
        return false;
     else if (dtDay < 1 || dtDay> 31) 
       return false;
     else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31) 
       return false;
     else if (dtMonth == 2){ 
       var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
       if (dtDay> 29 || (dtDay ==29 && !isleap)) 
      	 
      	 alert("Date format is not correct");
       document.f14.rdate.value="";
       document.f14.rdate.focus();
              return false;
        
        return true;
}
     
</script>

<script language="javascript" type="text/javascript">
 	function addRow()
	{	
 		var jcred=document.f15.jamount.value;
 		var jdeb=document.f15.jamount2.value;
 	
 		//alert(jcred+""+jdeb);

 		if(jdeb>jcred)
 		{
 			tableID="jvtable2";
 			
 			var table=document.getElementById(tableID); //table id
 			var rowCount=table.rows.length;  //row count of table
 			var row=table.insertRow(rowCount-2); 
 			var colCount=table.rows[0].cells.length; // column count
 			
 			for(var i=0;i<colCount;i++)
 			{
 				var newcell=row.insertCell(i);
 				newcell.innerHTML=table.rows[0].cells[i].innerHTML;
 				
 			//	newcell.childNodes[0].value="";
 			//	newcell.childNodes[0].selectedIndex=0;
 				
 				switch(newcell.childNodes[0].type)
 				{
 					case"text":newcell.childNodes[0].value="";
 						break;
 					case"select-one":newcell.childNodes[0].selectedIndex=0;
 						break;
 					case"text2":newcell.childNodes[0].value="";
 						break;
 					case"input":newcell.childNodes[0].selectIndex=0;
 				}
 			}
 		}
 		if(jcred>jdeb)
 		{
 			tableID="jvtable";
 			
 			var table=document.getElementById(tableID); //table id
 			var rowCount=table.rows.length;  //row count of table
 			var row=table.insertRow(rowCount-2); 
 			var colCount=table.rows[0].cells.length; // column count
 			
 			for(var i=0;i<colCount;i++)
 			{
 				var newcell=row.insertCell(i);
 				newcell.innerHTML=table.rows[0].cells[i].innerHTML;
 				
 			//	newcell.childNodes[0].value="";
 			//	newcell.childNodes[0].selectedIndex=0;
 				
 				switch(newcell.childNodes[0].type)
 				{
 					case"text":newcell.childNodes[0].value="";
 						break;
 					case"select-one":newcell.childNodes[0].selectedIndex=0;
 						break;
 					case"text2":newcell.childNodes[0].value="";
 						break;
 					case"input":newcell.childNodes[0].selectIndex=0;
 				}
 			}
 		}


	}
 </script>

</head>
<body>

<%--
<%
		List li1 = new ArrayList();
		li1 = (List) session.getAttribute("alldata");
		Iterator it = li1.listIterator();
		
%>
--%>

<form name="f15" action="JournalVoucherServlet" ONSUBMIT="return checkDate()">
<center><h2> Journal Voucher </h2> </center>
<table border=0 align="center">

<tr>
	<td> Voucher Entry Number </td>
	
			<%	
				DBConnection dbc=new DBConnection();	
				Connection con=dbc.getNewConnection();
		
			 	Statement st = null;
			 	ResultSet rs = null;
			 	String code2="JV1";
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select MAX(jname) from JournalVoucher2"); %>
       		   
       		 <%  while(rs.next()) 
       		 	{ 
       				String code1=rs.getString(1);
       				
       			    String[] part = code1.split("(?<=\\D)(?=\\d)");
	   		       
	   		       String p0=part[0];		       
	   		       int p1=Integer.parseInt(part[1]);
	   		      
	   		       p1++;
	   		       
	   		       code2=p0+""+p1;

            	} %>
  
	 		<% }
		   catch(Exception e){} %>
	<td> <input type="text" name="jentrynumber" value="<%=code2 %>"> </td>
</tr>

<tr>
	<td> date </td>
	
	<% SimpleDateFormat current = new SimpleDateFormat("dd/MM/yyyy"); %>	
	<td><input type="text" name="jdate" value="<%=current.format(new Date()) %>" /></td>
</tr>
</table>

<table align="center">
<tr>
	<td> Account Debited </td>
	
			<!--  jaccount --> 
	<!-- 	<td> <input type="text" name="jaccount"> </td> --> 
	<%--
	<%  for(int i=0; i<li1.size();) 
			{	%>
			<select>
				<option> <%= li1.get(i++) %> </option>
			</select>
			
		<%	} %>	
	--%>	
			 <%	
			 try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select ccode,cname from CustomerMaster"); %>
	<td> 
	  <select name="jaccount" id="jaccount" >
	  		<option> </option>
       		 <%  while(rs.next()){ %>
            		<option value="<%=rs.getString(1) %>" > <%=rs.getString(2)%> </option>
            		            		
        	<% } %>
  				
	 <% }
		catch(Exception e){} %>
	
		
	<%
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select scode,sname from SupplierMaster"); %>
	 	
        	<%  while(rs.next()){ %>
         		   <option value="<%=rs.getString(1) %>"><%= rs.getString(2)%></option>
         		 
        	<% } %>
 
	 <% }
		catch(Exception e){} %>
	
	<%
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select gcode,gname from GeneralLedger2"); %>
	 	
        	<%  while(rs.next()){ %>
         		   <option value="<%=rs.getString(1) %>"><%= rs.getString(2)%></option>
         		   
        	<% } %>
        </select>	
	</td>
	 <% }
		catch(Exception e){} %>
</tr>

<tr>
	<td colspan=2> Create New Account  </td>
</tr>
<tr>	
	<td colspan=2>  <a href="CustomerMaster.jsp"> Customer </a>
		 <a href="SupplierMaster.jsp"> Supplier </a> 
		 <a href="GeneralLedger.jsp"> GeneralLedger </a>
	</td>
</tr>

<tr>
	<td> Amount </td>
	<td> <input type="text" name="jamount"> </td>
</tr>
</table>

<table id="jvtable2" align="center">
<tr>
	<td> Account Credited </td>
	<!-- <td> <input type="text" name="jaccount2"> </td> -->
			<!--  jaccount2 --> 
	<%-- 
	 <%  for(int i=0; i<li1.size();) 
			{	%>
			<select>
				<option> <%= li1.get(i++) %> </option>
			</select>
			
		<%	} %>	
	--%>
	
			 <%	
			 try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select ccode,cname from CustomerMaster"); %>
	<td> 
	  <select name="jaccount2" id="jaccount2" >
	  		<option> </option>
       		 <%  while(rs.next()){ %>
            		<option value="<%=rs.getString(1) %>" > <%=rs.getString(2)%> </option>
            		            		
        	<% } %>
  				
	 <% }
		catch(Exception e){} %>
	
		
	<%
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select scode,sname from SupplierMaster"); %>
	 	
        	<%  while(rs.next()){ %>
         		   <option value="<%=rs.getString(1) %>"><%= rs.getString(2)%></option>
         		 
        	<% } %>
 
	 <% }
		catch(Exception e){} %>
	
	<%
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select gcode,gname from GeneralLedger2"); %>
	 	
        	<%  while(rs.next()){ %>
         		   <option value="<%=rs.getString(1) %>"><%= rs.getString(2)%></option>
         		   
        	<% } %>
        </select>	
	</td>
	 <% }
		catch(Exception e){} %>
</tr>

<tr>
	<td colspan=2> Create New Account  </td>
</tr>

<tr>	
	<td colspan=2>  <a href="CustomerMaster.jsp"> Customer </a>
		  <a href="SupplierMaster.jsp"> Supplier </a> 
		  <a href="GeneralLedger.jsp"> GeneralLedger </a>
	</td>
</tr>

<tr>
	<td> Amount </td>
	<td> <input type="text" name="jamount2" onblur="addRow()";> </td>
</tr>

<tr>
	<td> Narration </td>
	<td> <input type="text" name="jnarration"> </td>
</tr>

<tr>
	<td> <input type="submit" value="Submit" onclick="ValidateDateFormat(document.f15.jdate.value)"> </td>
</tr>

</table>
</form>

<script src="JS/DateValidation.js"></script>

</body>
</html>